package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.mybeans.form.FormBeanException;
import org.mybeans.form.FormBeanFactory;

import bean.Customer;
import bean.FundTransaction;
import bean.LoggedUser;
import bean.Model;
import bean.TransactionType;
import bean.UserType;
import businessBean.TransactionBean;
import dao.CustomerDAO;
import dao.FundTransactionDAO;
import formBean.CustomerNameForm;

public class TransactionHistoryAction extends Action {

	private FormBeanFactory<CustomerNameForm> formBeanFactory = FormBeanFactory.getInstance(CustomerNameForm.class);
	FundTransactionDAO transactionDAO = null;
	private CustomerDAO viewCustomerDAO;
	public TransactionHistoryAction(Model model)
	{
		transactionDAO = model.getFundTransactionDAO();
		viewCustomerDAO  = model.getCustomerDAO();
	}
	@Override
	public String getName() {
		return "transactionHistory.do";
	}
	
	private static boolean isBuyOrSellTransaction(FundTransaction transaction)
	{
		TransactionType type = transaction.getTransactionType();
		return (type.equals(TransactionType.BUY)||type.equals(TransactionType.SELL));
	}

	@Override
	public String perform(HttpServletRequest request) {
		LoggedUser user = (LoggedUser)request.getSession().getAttribute("user");
		List<String> errors = new ArrayList<String>();
		request.setAttribute("errors",errors);
		if(user.getUserType().equals(UserType.Customer))
		{
			return displayCustomerTransaction(request, Integer.toString(user.getId()));
		}
		else
		{
			CustomerNameForm form;
			try {
				form = formBeanFactory.create(request);
				request.setAttribute("form", form);
				if (!form.isPresent()) {
		            return "viewCustomerTransactionHistory.jsp";
		        }
				Customer customer = viewCustomerDAO.getCustomerByUsername(form.getUsername());
				
				if (customer == null) {
					errors.add("No such customer!");
					return "viewCustomerTransactionHistory.jsp";
				}
				else
				{
					return displayCustomerTransaction(request, Integer.toString(customer.getId()));
				}
			} catch (FormBeanException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return "viewCustomerTransactionHistory.jsp";
		}
	}
	private String displayCustomerTransaction(HttpServletRequest request,
			String userId) {
		List transactions = transactionDAO.getTransactionByCustomerId(userId);
		ArrayList<TransactionBean> transactionList = new ArrayList<TransactionBean>();
		if(transactions!=null && transactions.size()>0)
		{
			for(Object obj:transactions)
			{
				FundTransaction transaction = (FundTransaction)obj;
				if(transaction != null)
				{
					TransactionBean bean = new TransactionBean();
					if(transaction.getFund() != null)
					{
						bean.setFundName(transaction.getFund().getName());
					}
					else
					{
						bean.setFundName("");
					}
					bean.setOperation(transaction.getTransactionType().name());
					if(transaction.getShares()>0)
					{
						bean.setNumberOfShares(util.Util.showShares(transaction.getShares()));
					}
					else
					{
						bean.setNumberOfShares("");
					}
					if(transaction.getAmount()>0)
					{
						bean.setAmount(util.Util.showMoney(transaction.getAmount()));
					}
					else
					{
						bean.setAmount("");
					}
					double amount = (double)transaction.getAmount()*10;
					double shares = (double)transaction.getShares();
					double price = transaction.getShares() != 0?(amount/shares):0.0;
					if(price>0)
					{
						bean.setSharePrice(util.Util.showMoney(price));
					}
					if(transaction.getOrderDate()!=null)
					{
						bean.setOrderDate(util.Util.formatDate(transaction.getOrderDate()));
					}
					else
					{
						bean.setOrderDate("");
					}
					if(transaction.getExecuteDate()!=null)
					{
						bean.setTransactionDate(util.Util.formatDate(transaction.getExecuteDate()));
//						bean.setStatus("Completed");
					}
					else if(isBuyOrSellTransaction(transaction))
					{
						bean.setTransactionDate("Pending");
//						bean.setStatus("Pending");
					}
					else
					{
						bean.setTransactionDate("");
					}
					bean.setStatus(transaction.getStatus().name());
					transactionList.add(bean);
				}
			}
		}
		request.getSession().setAttribute("transactionList", transactionList);
		return "transactionHistory.jsp";
	}

	@Override
	public UserType getType() {
		return UserType.Both;
	}

}
